package com.aier.src.action;
import javax.annotation.Resource;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.servlet.http.Cookie;

import com.aier.classes.service.IClassService;
import com.aier.common.common.CommonCookie;
import com.aier.common.common.CommonSession;
import com.aier.common.common.Constant;
import com.aier.school.service.ISchoolService;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;

@Scope("prototype")
@Component("indexAction")
public class IndexAction extends BaseAction {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public int s;


    @Resource(name = "userService")
    private IUserService userService;
    @Resource(name="schoolService")
    private ISchoolService schoolService;
    @Resource(name="classService")
    private IClassService classService;
    
    @Override
    public String execute() {
        //cookie验证,是否有记住的登录状态,有就直接进入首页
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            String account=null,pwd=null;
            for (Cookie cookie : cookies) {
                if (CommonCookie.COOKIE_KEY_REMEMBER_LOGIN.equals(cookie.getName())) {
                    account = cookie.getValue();
                }else if(CommonCookie.COOKIE_KEY_PWD_LOGIN.equals(cookie.getName())){
                    pwd=cookie.getValue();
                }
            }
            if(StringUtil.isStringNull(account)||StringUtil.isStringNull(pwd)){
                return SUCCESS;
            }
            User user = userService.searchUserByCondition(account, pwd);
            if(user!=null){
                // 成功登录后记录session
                session.put(CommonSession.SESSION_ACCOUNT, user.getAccount());
                session.put(CommonSession.SESSION_ACCOUNT_ID, user.getUid());
                session.put(CommonSession.SESSION_ACCOUNT_NAME, user.getNickname());
                session.put(CommonSession.SESSION_ACCOUNT_SCHOOLLIST, schoolService.searchSchoolListByUid(user.getUid()));
                session.put(CommonSession.SESSION_ACCOUNT_CLASSLIST, classService.searchClassListByUid(user.getUid()));
                return "reload";
            }else{
                return SUCCESS;
            }
        }
        return SUCCESS;
    }

    /**
     * 用户未登录
     * 
     * @return
     */
    public String nologin() {
        s = Constant.num_30000;
        return SUCCESS;
    }

    public int getS() {
        return s;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public void setClassService(IClassService classService) {
        this.classService = classService;
    }

    

}
